package com.hy.frame.base;

import android.app.Application;
import android.text.TextUtils;

import com.hy.frame.util.ActManager;
import com.hy.frame.util.DateUtil;
import com.hy.frame.util.LogUtil;
import com.hy.frame.util.PmUtil;

/**
 * title Base Application
 * author HeYan
 * time 19-7-8 下午4:00
 * desc 全局需要注意多进程影响
 */
public abstract class BaseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        initAttrs();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        LogUtil.d(this.getClass(), "onTerminate");
    }

    /**
     * 是否开启日志
     */
    public boolean isLoggable() {
        return true;
    }

    /**
     * 是否开启Activity管理
     */
    public boolean isAutoStackActivity() {
        return true;
    }

    /**
     * 初始化进程逻辑
     */
    public void initAttrs() {
        LogUtil.enableLog(isLoggable());
        if (isAutoStackActivity()) {
            ActManager.getInstance().init(this);
        }
        int pid = android.os.Process.myPid();
        String processName = PmUtil.getCurProcessName(this);
        LogUtil.d(this.getClass(), String.format("init process=%s,pid=%s,time=%s", processName, pid, DateUtil.getNowDateStr()));
        //如果获取进程名称失败，或者进程名称与当前应用ID一致，表示是主进程
        if (TextUtils.isEmpty(processName) || TextUtils.equals(getPackageName(), processName)) {
            initAppForMainProcess();
            return;
        }
        //否则子进程
        initAppForOtherProcess(processName);
    }

    protected abstract void initAppForMainProcess();

    protected void initAppForOtherProcess(String process) {

    }
}
